﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using PopupControl;

namespace CEQuery
{
    public partial class PopRelation : UserControl
    {
        
        public List<CETable> Tables { get; set; }
        public PopRelation()
        {
            InitializeComponent();
            MinimumSize = Size;
            MaximumSize = new Size(Size.Width * 2, Size.Height * 2);
            DoubleBuffered = true;
            ResizeRedraw = true;

            //Tables = new List<CETables>();
            //CETables tabPanel1 = new CETables();
            //tabPanel1.TableName = "Table 1";
            //tabPanel1.Columns = new List<string>();
            //tabPanel1.Columns.Add("Column 1");
            //tabPanel1.Columns.Add("Column 2");
            //tabPanel1.Columns.Add("Column 3");
            //tabPanel1.Columns.Add("Column 4");
            //tabPanel1.Columns.Add("Column 5");
            //tabPanel1.Columns.Add("Column 6");
            //tabPanel1.Columns.Add("Column 7");
            //tabPanel1.Columns.Add("Column 1");
            //tabPanel1.Columns.Add("Column 2");
            //tabPanel1.Columns.Add("Column 3");
            //tabPanel1.Columns.Add("Column 4");
            //tabPanel1.Columns.Add("Column 5");
            //tabPanel1.Columns.Add("Column 6");
            //tabPanel1.Columns.Add("Column 7");

            //CETables tabPanel2 = new CETables();
            //tabPanel2.TableName = "Table 2";
            //tabPanel2.Columns = new List<string>();
            //tabPanel2.Columns.Add("Column 1");
            //tabPanel2.Columns.Add("Column 2");
            //tabPanel2.Columns.Add("Column 5");
            //tabPanel2.Columns.Add("Column 6");
            //tabPanel2.Columns.Add("Column 7");

            //Tables.Add(tabPanel1);
            //Tables.Add(tabPanel2);
        }
        protected override void WndProc(ref Message m)
        {
            if ((Parent as Popup).ProcessResizing(ref m))
            {
                return;
            }
            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool hasError = false;
            if (cboLeft.SelectedIndex == -1)
            {
                errRel.SetError(lblLeft, "Select a left table.");
                hasError = true;
            }
            else
            {
                errRel.SetError(lblLeft, string.Empty);
            }
            if (cboRight.SelectedIndex == -1)
            {
                errRel.SetError(lblRight, "Select a right table.");
                hasError = true;
            }
            else
            {
                errRel.SetError(lblRight, string.Empty);
            }
            if (cboJoin.SelectedIndex == -1)
            {
                errRel.SetError(lblJointype, "Select a join type.");
                hasError = true;
            }
            else
            {
                errRel.SetError(lblJointype, string.Empty);
            }
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1 && cboRight.SelectedItem.ToString() == cboLeft.SelectedItem.ToString())
            {
                errRel.SetError(lblRight, "Select a different table.");
                hasError = true;
            }
            else
            {
                errRel.SetError(lblJointype, string.Empty);
            }
            foreach (DataGridViewRow row in dgRel.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.ErrorText != string.Empty)
                    {
                        hasError = true;
                        break;
                    }
                }
            }
            if (!hasError)
            {
                CETableRelation rel = new CETableRelation();
                rel.LeftTableName = cboLeft.SelectedItem.ToString();
                rel.RightTableName = cboRight.SelectedItem.ToString();
                rel.JoinTypeName = cboJoin.SelectedItem.ToString(); ;
                rel.LeftColumns = new List<string>();
                rel.RightColumns = new List<string>();
                foreach (DataGridViewRow row in dgRel.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        rel.LeftColumns.Add(row.Cells[0].Value.ToString());
                        rel.RightColumns.Add(row.Cells[1].Value.ToString());
                    }
                }
                if (rel.LeftColumns.Count > 0 && rel.RightColumns.Count > 0)
                {
                    errRel.SetError(btnOk, string.Empty);
                    (Parent as Popup).Tag = rel;
                    (Parent as Popup).Close();
                }
                else
                {
                    errRel.SetError(btnOk, "Must provide left & right columns"); 
                }
            }
            else
            {
                errRel.SetError(btnOk, "Please correct the column mapping");
            }
        }

        private void PopRelation_Load(object sender, EventArgs e)
        {
            foreach (CETable pnl in Tables)
            {
                cboLeft.Items.Add(pnl.TableName);
                cboRight.Items.Add(pnl.TableName);
            }
        }

        private void dgRel_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1)
            {
                foreach (string leftCol in Tables.Find(delegate(CETable tblPanel) { return tblPanel.TableName == cboLeft.SelectedItem.ToString(); }).Columns)
                {
                    ((DataGridViewComboBoxCell)dgRel.Rows[e.RowIndex].Cells[0]).Items.Add(leftCol);
                }

                foreach (string rightCol in Tables.Find(delegate(CETable tblPanel) { return tblPanel.TableName == cboRight.SelectedItem.ToString(); }).Columns)
                {
                    ((DataGridViewComboBoxCell)dgRel.Rows[e.RowIndex].Cells[1]).Items.Add(rightCol);
                }
            }
        }

        private void cboLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgRel.Rows.Clear();
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1)
            {                
                dgRel.Rows.Add();
            }
        }

        private void cboRight_SelectedIndexChanged(object sender, EventArgs e)
        {
            dgRel.Rows.Clear();
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1)
            {                
                dgRel.Rows.Add();
            }
        }

        private void dgRel_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1)
            {
                foreach (string leftCol in Tables.Find(delegate(CETable tblPanel) { return tblPanel.TableName == cboLeft.SelectedItem.ToString(); }).Columns)
                {
                    ((DataGridViewComboBoxCell)dgRel.Rows[e.RowIndex].Cells[0]).Items.Add(leftCol);
                }

                foreach (string rightCol in Tables.Find(delegate(CETable tblPanel) { return tblPanel.TableName == cboRight.SelectedItem.ToString(); }).Columns)
                {
                    ((DataGridViewComboBoxCell)dgRel.Rows[e.RowIndex].Cells[1]).Items.Add(rightCol);
                }
            }
        }

        
        private void dgRel_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (cboLeft.SelectedIndex > -1 && cboRight.SelectedIndex > -1 && dgRel.Rows.Count > 2)
            {
                foreach (DataGridViewRow dr in dgRel.Rows)
                {
                    if (dr.Index != e.RowIndex && dr.Cells[e.ColumnIndex].Value != null && dgRel.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    {
                        if (dr.Cells[e.ColumnIndex].Value.ToString() == dgRel.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
                        {
                            dgRel.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Column already in use";
                            break;
                        }
                        else
                        {
                            dgRel.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = string.Empty;
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            (Parent as Popup).Tag = null;
            (Parent as Popup).Close();
        }

        
    }
    //[Serializable]
    
}
